﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TRPOP.CRM.Web.Controls
{
    public partial class UC_Pager : System.Web.UI.UserControl
    {
        #region Private Instances

        //private int     p_intTotalPages = 0;
        private int p_intDisplayedPages = 10;
        //private int p_intRecordCount = 0;
        private int p_intPageSize = 15;

        protected static readonly object EventPageChanged = new object();


        #endregion

        #region Public Properties


        public event EventHandler PageChanged
        {
            add
            {
                Events.AddHandler(EventPageChanged, value);
            }
            remove
            {
                Events.RemoveHandler(EventPageChanged, value);
            }
        }


        public int TotalPages
        {
            get
            {
                //return p_intTotalPages;

                object obj = ViewState["TotalPages"];
                return (null == obj) ? 0 : int.Parse(obj.ToString());
            }
            set
            {
                //p_intTotalPages = value;
                ViewState["TotalPages"] = value;
            }
        }

        public int RecordCount
        {
            get
            {
                object obj = ViewState["RecordCount"];
                return (null == obj) ? 0 : int.Parse(obj.ToString());
                //return p_intRecordCount;
            }
            set
            {
                ViewState["RecordCount"] = value;
                //p_intRecordCount = value;
            }
        }

        public int PageSize
        {
            get
            {
                return p_intPageSize;
            }
            set
            {
                p_intPageSize = value;
            }
        }

        public int DisplayedPages
        {
            get
            {
                return p_intDisplayedPages;
            }
            set
            {
                p_intDisplayedPages = value;
            }
        }

        public int CurrentPage
        {
            get
            {

                if (Request.Form["__EVENTTARGET"] == "Page")
                {
                    ViewState["CurrentPage"] = Request.Form["__EVENTARGUMENT"];
                }
                object obj = ViewState["CurrentPage"];
                //return (null == obj) ? 1 : TypeParse.ParseInt(obj.ToString());

                if (null == obj)
                {
                    return 1;
                }
                else if (int.Parse(obj.ToString()) > TotalPages && TotalPages != 0)
                {
                    return TotalPages;
                }
                else if (int.Parse(obj.ToString()) < 1)
                {
                    return 1;
                }
                else
                {
                    return int.Parse(obj.ToString());
                }
            }
            set
            {
                ViewState["CurrentPage"] = value;
            }
        }

        #endregion

        #region Event Handlers


        /// <summary>
        /// 定义Onclick
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnPageChange(EventArgs e)
        {
            EventHandler clickHandler = (EventHandler)Events[EventPageChanged];
            if (clickHandler != null)
            {
                clickHandler(this, e);
            }
        }

        /// <summary>
        /// 引发PostBack事件
        /// </summary>
        /// <param name="eventArgument"></param>
        public void RaisePostBackEvent(string eventArgument)
        {
            OnPageChange(new EventArgs());
            //Reader();
        }
        //protected override void OnPreRender ( EventArgs e )
        //{
        //    //if (Request.Form["__EVENTTARGET"] == "Page")
        //    //{
        //    //    RaisePostBackEvent( "RaisePostBackEvent" );
        //    //}
        //    //else
        //    //{
        //    //    this.CurrentPage = 1;
        //    //} 

        //    //Reader();
        //    //base.OnInit( e );
        //}

        protected void Page_Load(object sender, EventArgs e)
        {


        }
        protected override void Render(HtmlTextWriter writer)
        {
            //if ( Request.Form [ "__EVENTTARGET" ] == "Page" )
            //{
            //    RaisePostBackEvent ( "RaisePostBackEvent" );
            //}
            //else
            //{
            //    //this.CurrentPage = 1;
            //    this.CurrentPage = ViewState [ "CurrentPage" ] == null ? 1 : int.Parse ( ViewState [ "CurrentPage" ].ToString ( ) );

            //}
            try
            {
                this.Table1.Rows.RemoveAt(0);
            }
            catch
            {
            }
            Reader();
            base.Render(writer);
        }
        protected override void OnPreRender(EventArgs e)
        {
            if (Request.Form["__EVENTTARGET"] == "Page")
            {
                RaisePostBackEvent("RaisePostBackEvent");
            }
            else
            {
                //this.CurrentPage = 1;
                this.CurrentPage = ViewState["CurrentPage"] == null ? 1 : int.Parse(ViewState["CurrentPage"].ToString());

            }
            //try
            //{
            //    this.Table1.Rows.RemoveAt ( 0 );
            //}
            //catch
            //{
            //}
            // Reader ( );
            base.OnPreRender(e);
        }

        protected void Reader()
        {
            if (!p_intPageSize.Equals(0) && RecordCount > 0)
            {
                TotalPages = RecordCount % p_intPageSize == 0 ? RecordCount / p_intPageSize : RecordCount / p_intPageSize + 1;
            }

            int p_intTotalPages = TotalPages;


            int currentPageNo = CurrentPage;


            if (currentPageNo > p_intTotalPages)
            {
                currentPageNo = p_intTotalPages;
            }
            else if (currentPageNo < 1)
            {
                currentPageNo = 1;
            }

            int offset = p_intDisplayedPages / 2 - 1;



            TableRow row = new TableRow();
            //row.CssClass = "smalltxt";
            this.Table1.Rows.Add(row);

            TableCell cellPageInfo = new TableCell();
            cellPageInfo.Text = "共" + RecordCount + "条记录，" + currentPageNo + "/" + p_intTotalPages + "页";
            cellPageInfo.CssClass = "this-page";
            row.Cells.Add(cellPageInfo);

            if (p_intTotalPages <= p_intDisplayedPages)
            {
                for (int i = 1; i <= p_intTotalPages; i++)
                {
                    TableCell cell = new TableCell();
                    HyperLink link = new HyperLink();
                    link.EnableTheming = true;
                    link.Text = i.ToString();
                    if (currentPageNo == i)
                    {
                        link.CssClass = "now-page";
                    }
                    else
                    {
                        link.NavigateUrl = "javascript:__doPostBack('Page','" + i + "')";
                    }
                    cell.Controls.Add(link);
                    row.Cells.Add(cell);
                }
            }
            else
            {
                if (currentPageNo == 1)
                {
                    for (int i = 1; i <= p_intDisplayedPages; i++)
                    {
                        TableCell cell = new TableCell();
                        HyperLink link = new HyperLink();
                        link.EnableTheming = true;
                        if (i < p_intDisplayedPages - 1)
                        {
                            link.Text = i.ToString();
                            if (i == currentPageNo)
                            {
                                link.CssClass = "now-page";
                            }
                            else
                            {
                                link.NavigateUrl = "javascript:__doPostBack('Page','" + i + "')";
                            }
                        }
                        else if (i == p_intDisplayedPages - 1)
                        {
                            link.Text = "..." + Convert.ToString(p_intTotalPages);
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + p_intTotalPages + "')";
                        }
                        else if (i == p_intDisplayedPages)
                        {
                            //link.ImageUrl = "~/Images/next1.gif";
                            //link.SkinID="PagerNext";
                            link.Text = "下一页";
                            link.CssClass = "page-next";
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + (currentPageNo + 1) + "')";
                        }
                        cell.Controls.Add(link);
                        row.Cells.Add(cell);
                    }
                }
                else if (currentPageNo > 1 && currentPageNo <= offset)
                {
                    for (int i = 1; i <= p_intDisplayedPages; i++)
                    {
                        TableCell cell = new TableCell();
                        HyperLink link = new HyperLink();
                        link.EnableTheming = true;
                        if (i == 1)
                        {
                            //link.ImageUrl = "~/Images/fore.gif";
                            //link.SkinID="PagerFore";
                            link.Text = "上一页";
                            link.CssClass = "page-fore";
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + (currentPageNo - 1) + "')";
                        }
                        else if (i > 1 && i < p_intDisplayedPages - 1)
                        {
                            link.Text = Convert.ToString(i - 1);
                            if (currentPageNo == i - 1)
                            {
                                link.CssClass = "now-page";
                            }
                            else
                            {
                                link.NavigateUrl = "javascript:__doPostBack('Page','" + (i - 1) + "')";
                            }
                        }
                        else if (i == p_intDisplayedPages - 1)
                        {
                            link.Text = "..." + Convert.ToString(p_intTotalPages);
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + p_intTotalPages + "')";
                        }
                        else if (i == p_intDisplayedPages)
                        {
                            //link.ImageUrl = "~/Images/next1.gif";
                            //link.SkinID = "PagerNext";
                            link.Text = "下一页";
                            link.CssClass = "page-next";
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + (currentPageNo + 1) + "')";
                        }
                        cell.Controls.Add(link);
                        row.Cells.Add(cell);
                    }
                }
                else if (currentPageNo > offset && currentPageNo <= p_intTotalPages - offset)
                {
                    for (int i = 1; i <= p_intDisplayedPages; i++)
                    {
                        TableCell cell = new TableCell();
                        HyperLink link = new HyperLink();
                        link.EnableTheming = true;
                        if (i == 1)
                        {
                            //link.ImageUrl = "~/Images/fore.gif";
                            //link.SkinID = "PagerFore";
                            link.Text = "上一页";
                            link.CssClass = "page-fore";
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + (currentPageNo - 1) + "')";
                        }
                        else if (i == 2)
                        {
                            link.Text = "1...";
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + 1 + "')";
                        }
                        else if (i > 2 && i < p_intDisplayedPages - 1)
                        {
                            if (p_intDisplayedPages % 2 == 0)
                            {
                                link.Text = Convert.ToString(currentPageNo - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i);

                                if (currentPageNo == currentPageNo - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i)
                                {
                                    link.CssClass = "now-page";
                                }
                                else
                                {
                                    link.NavigateUrl = "javascript:__doPostBack('Page','" + (currentPageNo - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i) + "')";
                                }
                            }
                            else
                            {
                                link.Text = Convert.ToString(currentPageNo - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i - 1);
                                if (currentPageNo == currentPageNo - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i - 1)
                                {
                                    link.CssClass = "now-page";
                                }
                                else
                                {
                                    link.NavigateUrl = "javascript:__doPostBack('Page','" + (currentPageNo - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i - 1) + "')";
                                }
                            }
                        }
                        else if (i == p_intDisplayedPages - 1)
                        {
                            link.Text = "..." + Convert.ToString(p_intTotalPages);
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + p_intTotalPages + "')";
                        }
                        else if (i == p_intDisplayedPages)
                        {
                            //link.ImageUrl = "~/Images/next1.gif";
                            //link.SkinID = "PagerNext";
                            link.Text = "下一页";
                            link.CssClass = "page-next";
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + (currentPageNo + 1) + "')";
                        }
                        cell.Controls.Add(link);
                        row.Cells.Add(cell);
                    }
                }
                else if (currentPageNo > p_intTotalPages - offset && currentPageNo < p_intTotalPages)
                {
                    for (int i = 1; i <= p_intDisplayedPages; i++)
                    {
                        TableCell cell = new TableCell();
                        HyperLink link = new HyperLink();
                        link.EnableTheming = true;
                        if (i == 1)
                        {
                            //link.ImageUrl = "~/Images/fore.gif";
                            //link.SkinID = "PagerFore";
                            link.Text = "上一页";
                            link.CssClass = "page-fore";
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + (currentPageNo - 1) + "')";
                        }
                        else if (i == 2)
                        {
                            link.Text = "1...";
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + (1) + "')";
                        }
                        else if (i > 2 && i < p_intDisplayedPages)
                        {
                            link.Text = Convert.ToString(p_intTotalPages - p_intDisplayedPages + 1 + i);

                            if (currentPageNo == p_intTotalPages - p_intDisplayedPages + 1 + i)
                            {
                                link.CssClass = "now-page";
                            }
                            else
                            {
                                link.NavigateUrl = "javascript:__doPostBack('Page','" + (p_intTotalPages - p_intDisplayedPages + 1 + i) + "')";
                            }
                        }
                        else if (i == p_intDisplayedPages)
                        {
                            //link.ImageUrl = "~/Images/next1.gif";
                            //link.SkinID = "PagerNext";
                            link.Text = "下一页";
                            link.CssClass = "page-next";
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + (currentPageNo + 1) + "')";
                        }
                        cell.Controls.Add(link);
                        row.Cells.Add(cell);
                    }
                }

                else if (currentPageNo == p_intTotalPages)
                {
                    for (int i = 1; i <= p_intDisplayedPages; i++)
                    {
                        TableCell cell = new TableCell();
                        HyperLink link = new HyperLink();
                        link.EnableTheming = true;
                        if (i == 1)
                        {
                            //link.ImageUrl = "~/Images/fore.gif";
                            //link.SkinID = "PagerFore";
                            link.Text = "上一页";
                            link.CssClass = "page-fore";
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + (currentPageNo - 1) + "')";
                        }
                        else if (i == 2)
                        {
                            link.Text = "1...";
                            link.NavigateUrl = "javascript:__doPostBack('Page','" + (1) + "')";
                        }
                        else if (i > 2 && i <= p_intDisplayedPages)
                        {
                            link.Text = Convert.ToString(p_intTotalPages - p_intDisplayedPages + i);
                            if (currentPageNo == p_intTotalPages - p_intDisplayedPages + i)
                            {
                                link.CssClass = "now-page";
                            }
                            else
                            {
                                link.NavigateUrl = "javascript:__doPostBack('Page','" + (p_intTotalPages - p_intDisplayedPages + i) + "')";
                            }
                        }
                        cell.Controls.Add(link);
                        row.Cells.Add(cell);
                    }
                }
            }

            if (p_intTotalPages > 1)
            {

                TableCell pcelljump = new TableCell();
                pcelljump.Text = "跳转";
                pcelljump.CssClass = "jump-page";
                row.Cells.Add(pcelljump);


                TableCell pcell = new TableCell();
                pcell.Text = "<input type=\"text\" id=\"" + this.ClientID + "\" class=\"page-text\" style=\"width:30px\" maxlength=\"4\" value=" + currentPageNo.ToString() + " onKeyDown=\"if(event.keyCode==13){__doPostBack('Page',parseInt(this.value) > 0 ? parseInt(this.value) : 1);  return false;}\">";
                row.Cells.Add(pcell);


                TableCell pcell2 = new TableCell();

                HtmlInputSubmit ib1 = new HtmlInputSubmit();
                //ib1.EnableTheming = true;                
                //ib1.Src = "../App_Themes/Default/Images/find.gif";                
                ib1.Attributes.Add("Value", "");
                ib1.Attributes.Add("class", "page-find");
                ib1.Attributes.Add("onClick", "pagenumber = document.getElementById('" + this.ClientID + "').value;__doPostBack('Page',pagenumber);  return false;");
                pcell2.Controls.Add(ib1);


                //只有这个元素会产生 __doPostBack 方法，所以加上这个并隐藏起来。
                HtmlInputImage ib2 = new HtmlInputImage();
                ib2.Attributes.Add("style", "display:none");
                pcell2.Controls.Add(ib2);

                row.Cells.Add(pcell2);
            }

        }



        #endregion

        #region Public Methods

        public void Page_ReLoad()
        {
            CurrentPage = 1;
            // Reader ( );
        }

        #endregion
    }
}